﻿using System;
using System.Web;
using System.Web.Mvc;
using TidyBlog.MVC;
using TidyBlog.Entity;
using TidyBlog.BLL;
using TidyBlog.MVC.WWW.Models;

namespace TidyBlog.MVC.WWW.Controllers
{
    /// <summary>
    /// 评论管理控制器 /Manage_Comment/{Action}
    /// </summary>
    public class Manage_CommentController : ManageController
    {
        /// <summary>
        /// 评论列表
        /// </summary>
        /// <param name="page">页码</param>
        /// <returns></returns>
        public ActionResult List(int? page)
        {
            var result = CommentBLL.List(null, false, 20, page.GetValueOrDefault());

            return View(new ListWithPaginator<Comment>()
            {
                Result = result,
                ResultPaginator = new Paginator(result.CurrentPage, result.PageCount, Request.Path + "?page={0}")
            });
        }

        /// <summary>
        /// 批量操作评论
        /// </summary>
        /// <param name="action">动作</param>
        /// <param name="commentIds">评论编号</param>
        /// <returns></returns>
        public ActionResult Batch(string action, string[] commentIds)
        {
            var ids = String.Join<string>(",", commentIds);

            switch (action)
            {
                case "review":
                    CommentBLL.Review(ids);
                    RespondentInfo.Success(this.ControllerContext, new OperInfo()
                    {
                        Message = "已审核指定评论",
                        Link = Url.Action("List"),
                        LinkText = "返回列表"
                    });
                    break;

                case "delete":
                    CommentBLL.Delete(ids);
                    RespondentInfo.Success(this.ControllerContext, new OperInfo()
                    {
                        Message = "已删除指定评论",
                        Link = Url.Action("List"),
                        LinkText = "返回列表"
                    });
                    break;
            }

            return new EmptyResult();
        }

        /// <summary>
        /// 输出未审核的评论数
        /// </summary>
        /// <returns></returns>
        public ActionResult UnreviewedCount()
        {
            Response.ContentType = "text/plain";
            Response.Write(CommentBLL.GetUnreviewedCount());

            return new EmptyResult();
        }
    }
}